package com.gsq.learning.mybatisplugin.controller;

import com.gsq.learning.mybatisplugin.cache.OneLevelCache;
import com.gsq.learning.mybatisplugin.cache.TwoLevelCache;
import com.gsq.learning.mybatisplugin.domain.User;
import com.gsq.learning.mybatisplugin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author guishangquan
 * @date 2018/8/23
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/findByName")
    public User findByName(@RequestParam("name") String name) {
        return userService.findByName(name);
    }

    @GetMapping("/findByNameAndAge")
    public User findByNameAndAge(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        return userService.findByNameAndAge(name, age);
    }

    @Autowired
    private OneLevelCache oneLevelCache;
    @Autowired
    private TwoLevelCache twoLevelCache;

    @GetMapping("cache")
    public String testCache(@RequestParam("id") Integer id, @RequestParam("level") Integer level) {
        if (level == 1) {
            if (id == 1) {
                oneLevelCache.test1();
            } else if (id == 2) {
                oneLevelCache.test2();
            } else if (id == 3) {
                oneLevelCache.test3();
            }
        } else if (level == 2) {
            if (id == 1) {
                twoLevelCache.test1();
            } else if (id == 2) {
                twoLevelCache.test2();
            } else if (id == 3) {
                twoLevelCache.test3();
            } else if (id == 4) {
                twoLevelCache.test4();
            } else if (id == 5) {
                twoLevelCache.test5();
            }
        }
        return "success";
    }
}
